<?php
/**
 * IndexController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_BannerController extends Thaind_Controller_Action
{
	var $_arrParam;
    public function init()
    {
//    	Truyền các tham số nhận được ra view

    	$this->_arrParam = $this->_request->getParams();
		if (!isset($this->_arrParam['os']) || !$this->_arrParam['os']) {
    		$this->_arrParam['os'] = 1;
    	}
    	$this->view->arrParam = $this->_arrParam;
    }

    public function preDispatch(){
    	
//    	Chọn layout mặc định cho các action trong controller
    	
		$template_path = TEMPLATE_PATH . "/admin/default";
		$this->loadTemplate($template_path,'template.ini','template');

//		Kiểm tra đăng nhập

		$auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity()){
			$this->_arrParam['action'] = 'login';
			$this->_forward('login','index','admin',$this->_arrParam);
		}
    	$identity = $auth->getIdentity();
    	
    	if (!$identity->admin && !in_array(3,explode("|",$identity->permission))){
    		$this->_redirect("/admin");
    	}
    }
    
    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {
    	Zend_Loader::loadClass("Admin_Models_BannerModel");
    	Zend_Loader::loadClass("Admin_Models_ProductModel");
    	$banner_model = new Admin_Models_BannerModel();
    	$product_model = new Admin_Models_ProductModel();
		if ($this->_request->isPost()){
        	$mode = $this->_request->getParam('mode');        	
        	switch ($mode) {
        		case 'add':
        			$data = array(	'product_id'	=> $this->_request->getParam('product_id'),
        							'type'			=> $this->_request->getParam('type'));
        			$banner_model->insert($data);
	        		break;
        		case 'delete':
        			$id = $this->_request->getParam('product_id');
        			$where = $banner_model->getAdapter()->quoteInto('id = ?', $id);
        			$banner_model->delete($where);
        			break;        		
        	}
        }
    	$banner_list = $banner_model->getBannerList();
    	$product_list = $product_model->getProdctList($this->_arrParam);
    	$this->view->product_list = array();
		foreach ($product_list as $product) {
			$this->view->product_list[$product['id']] = $product['name'];
		}
    	$this->view->banner_list = $banner_list;    	
    }
}
